package com.liquanhack.test;

import java.util.*;


public class test {
    public static void main(String args[]) {
        for (int i=0;i<1;i++) {
            List<Integer> aa =new ArrayList<>();
            try {
                aa = MakeRedPack(2000,7);
            } catch (Exception e) {
                System.out.println("生成红包异常:"+e.getMessage());
            }
            int to=0;
            for(int a : aa){
                to=to+a;
                float yuan=(float) a/100;
                System.out.println("抽取红包金额为："+yuan+"元");
            }
            System.out.println("总金额为："+(float) to/100+"元,包："+aa.size());
        }
    }

    public static List<Integer> MakeRedPack(int total, int num) throws Exception {
        if(num==0){
            throw new Exception("红包包数错误");
        }
        //先给每个人平均值
        int pin=total/num;//平均
        if(pin==0){
            throw new Exception("红包金额不够分");
        }
        int yu=total % num;//余数
        int[] arr=new int[num];
        Arrays.fill(arr,pin);
        arr[0]+=yu;//把余数加到第一包
        Random r = new Random();
        List<Integer> rec=new ArrayList<>();
        for (int i = 0; i < num; i++) {
            int temp=r.nextInt(arr[i]);//随机抽取的金额
            if(temp==0){
                System.out.println("O");
                continue;
            }
            int index=(i==(num-1)?0:i+1);//抽取的金额给哪个索引
            arr[i]-=temp;//抽取随机金额
            arr[index]+=temp;//抽取的金额给下一包
        }

        for (int i = 0; i < num; i++) {
            rec.add(arr[i]);
        }
        //打乱顺序
        Collections.shuffle(rec);
        return rec;
    }
}
